package licode.unisop.single.user.infrastructure.repository.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import licode.unisop.provider.info.UserIdInfo;
import licode.unisop.provider.vo.SiAccount;
import licode.unisop.single.user.application.entity.UserAccountEntity;
import licode.unisop.single.user.application.gateway.UserAccountDao;
import licode.unisop.single.user.infrastructure.assembler.UniUserSingleAssembler;
import licode.unisop.single.user.infrastructure.repository.mapper.UserAccountMapper;
import licode.unisop.single.user.infrastructure.repository.po.UserAccountPO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * uni-user-single
 *
 * @author licode
 * @date 2016-08-27 15:28:35
 */
@Component
public class UserAccountDaoImpl
        extends ServiceImpl<UserAccountMapper, UserAccountPO>
        implements UserAccountDao {
    @Resource
    private UniUserSingleAssembler uniUserSingleAssembler;

    @Override
    public void add(UserAccountEntity item) {
        super.save(uniUserSingleAssembler.fromUserAccountEntity(item));
    }

    @Override
    public void addBatch(List<UserAccountEntity> items) {
        List<UserAccountPO> userAccounts = new ArrayList<>();
        items.forEach(item -> userAccounts.add(uniUserSingleAssembler.fromUserAccountEntity(item)));
        super.saveBatch(userAccounts);
    }

    @Override
    public void update(UserAccountEntity item) {
        super.updateById(uniUserSingleAssembler.fromUserAccountEntity(item));
    }

    @Override
    public void remove(UserIdInfo idInfo) {
        super.lambdaUpdate()
                .eq(UserAccountPO::getRealmId, idInfo.getRealmId())
                .eq(UserAccountPO::getUserId, idInfo.getUserId())
                .remove();
    }

    @Override
    public void removeWithId(Serializable id) {
        super.removeById(id);
    }

    @Override
    public String findByAccount(SiAccount info) {
        UserAccountPO accountPo = null;
        AccountCategory cg = AccountCategory.build(info.getCategory());
        if (cg.isMobile()) {
            accountPo = super.lambdaQuery()
                    .eq(UserAccountPO::getRealmId, info.getRealmId())
                    .eq(UserAccountPO::getMobile, info.getAccount())
                    .one();
        } else if (cg.isEmail()) {
            accountPo = super.lambdaQuery()
                    .eq(UserAccountPO::getRealmId, info.getRealmId())
                    .eq(UserAccountPO::getEmail, info.getAccount())
                    .one();
        } else if (cg.isAccount()) {
            accountPo = super.lambdaQuery()
                    .eq(UserAccountPO::getRealmId, info.getRealmId())
                    .eq(UserAccountPO::getUserName, info.getAccount())
                    .one();
        } else if (cg.isWx()) {
            accountPo = super.lambdaQuery()
                    .eq(UserAccountPO::getRealmId, info.getRealmId())
                    .eq(UserAccountPO::getWxUnionId, info.getAccount())
                    .one();
        }
        if (null == accountPo) {
            return null;
        }

        return accountPo.getUserId();
    }

    @Override
    public UserAccountEntity findAccount(UserIdInfo idInfo) {
        return uniUserSingleAssembler.fromUserAccount(super.lambdaQuery()
                .eq(UserAccountPO::getRealmId, idInfo.getRealmId())
                .eq(UserAccountPO::getUserId, idInfo.getUserId())
                .one());
    }
}
